package com.neuedu.service.Imp;

import com.neuedu.dao.FoodCategoryDao;
import com.neuedu.dao.Imp.FoodCategoryDaoImp;
import com.neuedu.dao.Imp.ShopDaoImp;
import com.neuedu.dao.ShopDao;
import com.neuedu.pojo.FoodCategory;
import com.neuedu.pojo.PageResult;
import com.neuedu.pojo.Shop;
import com.neuedu.pojo.vo.FoodCategoryVO;
import com.neuedu.service.FoodCategoryService;

import java.util.ArrayList;
import java.util.List;

public class FoodCategoryServiceImp implements FoodCategoryService {
    private FoodCategoryDao foodCategoryDao = new FoodCategoryDaoImp();
    private ShopDao shopDao = new ShopDaoImp();

    @Override
    public PageResult<List<FoodCategoryVO>> getFoodCategory(String currentPage, String pageSize,String shopId) {
        try{
            List<FoodCategory> list = foodCategoryDao.getFoodCategory(currentPage,pageSize,shopId);
            List<FoodCategoryVO> vos = new ArrayList<>();
            for (FoodCategory foodCategory : list) {
                FoodCategoryVO vo = new FoodCategoryVO();
                Integer sid = foodCategory.getShopId();
                // 通过店铺的id去查询店铺的信息，需要获取到店铺的名称
                Shop shop = shopDao.getShopName(sid+"");
                // 封装数据
                vo.setShopName(shop.getShopName());
                vo.setCategoryId(foodCategory.getCategoryId());
                vo.setCategoryName(foodCategory.getCategoryName());
                vo.setShopId(foodCategory.getShopId());

                // 将封装好的vo对象添加集合中
                vos.add(vo);
            }


            Long l = foodCategoryDao.countFoodCategory(shopId);

            PageResult<List<FoodCategoryVO>> pageResult = new PageResult<List<FoodCategoryVO>>();

            pageResult.setTotal(l);
            pageResult.setDatas(vos);

            return pageResult;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer addFoodCategory(String categoryName, String shopId) {
        try{
            return foodCategoryDao.addFoodCategory(categoryName,shopId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer updateFoodCategory(String categoryId, String categoryName, String shopId) {
        try{
            return foodCategoryDao.updateFoodCategory(categoryId, categoryName,shopId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer delFoodCategory(String categoryId) {
        try{
            return foodCategoryDao.delFoodCategory(categoryId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
